package com.example.pt.mapper;

import com.example.pt.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 评论Mapper接口
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    
    /**
     * 查询种子下的所有一级评论（非回复）
     * @param torrentId 种子ID
     * @return 评论列表
     */
    @Select("SELECT * FROM comment WHERE torrent_id = #{torrentId} AND parent_id IS NULL AND status = 0 ORDER BY create_time DESC")
    List<Comment> selectRootCommentsByTorrentId(@Param("torrentId") Integer torrentId);
    
    /**
     * 查询评论的所有回复
     * @param parentId 父评论ID
     * @return 回复列表
     */
    @Select("SELECT * FROM comment WHERE parent_id = #{parentId} AND status = 0 ORDER BY create_time ASC")
    List<Comment> selectRepliesByParentId(@Param("parentId") Integer parentId);
    
    /**
     * 查询种子下的所有评论（包括根评论和回复）
     * @param torrentId 种子ID
     * @return 评论列表
     */
    @Select("SELECT * FROM comment WHERE torrent_id = #{torrentId} AND status = 0")
    List<Comment> selectAllByTorrentId(@Param("torrentId") Integer torrentId);
    
    /**
     * 根据评论ID和用户ID查询是否已点赞
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 点赞记录数
     */
    @Select("SELECT COUNT(*) FROM comment_like WHERE comment_id = #{commentId} AND user_id = #{userId} AND status = 0")
    int checkUserLiked(@Param("commentId") Integer commentId, @Param("userId") Integer userId);

    /**
     * 根据用户uid查询所有的评论
     * @param userId 用户id
     * @return 评论列表
     */
    @Select("SELECT * FROM comment WHERE user_id = #{userId} AND status = 0")
    List<Comment> searchUserComments(@Param("userId") Integer userId);
} 